package org.devocean.groupbuying.service.impl;

import java.util.Calendar;
import java.util.List;

import org.devocean.groupbuying.dao.PurchaseDAO;
import org.devocean.groupbuying.domain.Cart;
import org.devocean.groupbuying.domain.CartItem;
import org.devocean.groupbuying.domain.Customer;
import org.devocean.groupbuying.domain.Purchase;
import org.devocean.groupbuying.domain.PurchaseItem;
import org.devocean.groupbuying.service.CustomerService;
import org.devocean.groupbuying.service.DiscountService;
import org.devocean.groupbuying.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PurchaseServiceImpl implements PurchaseService {

	@Autowired
	private PurchaseDAO purchaseDAO;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private DiscountService discountService;

	@Transactional(readOnly = true)
	public Purchase getPurchaseById(Integer id) {

		return purchaseDAO.findPurchaseById(id);
	}

	@Transactional
	public Purchase addOrUpdatePurchase(Purchase purchase) {
		if (purchase.getPurchaseID() == null) {
			return purchaseDAO.savePurchase(purchase);
		} else {
			return purchaseDAO.updatePurchase(purchase);
		}
	}

	@Transactional
	public void removePurchase(Integer id) {
		purchaseDAO.deletePurchase(id);

	}

	@Transactional
	public long countPurchases() {
		return purchaseDAO.countPurchase();
	}

	@Transactional
	public List<Purchase> getAllPurchases() {
		return purchaseDAO.findAllPurchases();
	}
	@Transactional
	public Purchase addPurchaseFromCart(Integer customerID, Cart cart) {
		Purchase purchase = new Purchase();
		purchase.setCustomer(customerService.getCustomerById(customerID));
		purchase.setAmount(cart.getAmount());
		purchase.setPurchaseDate(Calendar.getInstance().getTime());
		// purchase.setPurchaseInfo(cart.getCartInfo());

		for (CartItem item : cart.getCartItems()) {
			PurchaseItem pItem = new PurchaseItem();

			// Price
			pItem.setUnitPrice(item.getUnitPrice());
			pItem.setQuantity(item.getQuantity());
			pItem.setAmount(item.getAmount());
			// Status
			pItem.setStatus("undefined");
			pItem.setStatusDate(Calendar.getInstance().getTime());
			// Associations
			pItem.setDiscount(discountService.getDiscountById(item
					.getDiscountID()));
			pItem.setPurchase(purchase);
			// Register under Purchase
			purchase.getItems().add(pItem);

		}
		purchase = addOrUpdatePurchase(purchase);
//		purchase = purchaseDAO.savePurchase(purchase);
		return purchase;
	}
	@Transactional
	public List<Purchase> PurchasesByCustomer(Integer customerID) {
		return purchaseDAO.findPurchasesByCustomer(customerID);
	}

	public List<Purchase> getPurchaseByCustomerAndLang(Customer customer,String lang) {
		
		return purchaseDAO.findPurchasesByCustomer(customer.getCustomerID(),lang);
	}

	public Long countPurchaseByCustomer(Customer customer) {
		
		return purchaseDAO.countPurchaseByCustomer(customer.getCustomerID());
	}
}
